package com.example.yunifang.adapter;

import java.util.List;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yunifang.ClassSeachActivity;
import com.example.yunifang.R;
import com.example.yunifang.db.SerachHistorySqlliteOpenHelper;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class ClassSerachHistoryAdapter extends BaseAdapter {
	ClassSeachActivity Seachactivity = new ClassSeachActivity();
	private Context context;
	private List<String> alllist;
	private TextView serach_history_item_tt;
	private ImageView serach_history_item_im;
	private int count;
	private ClassSerachHistoryAdapter serachHistoryAdapter;

	public ClassSerachHistoryAdapter(Context context, List<String> alllist, int count) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.alllist = alllist;
		this.count = count;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return count;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return alllist.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = View.inflate(context, R.layout.search_history_item, null);
		serach_history_item_tt = (TextView) view
				.findViewById(R.id.serach_history_item_tt);
		serach_history_item_im = (ImageView) view
				.findViewById(R.id.serach_history_item_im);

		serach_history_item_im.setOnClickListener(new OnClickListener() {

			private int ids;
			private SerachHistorySqlliteOpenHelper helper;
			private ClassSerachHistoryAdapter serachHistoryAdapter;
			private String serach_name;

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "���Ե��", 0).show();
				alllist.remove(position);
				count = alllist.size();
				serachHistoryAdapter = new ClassSerachHistoryAdapter(context,
						alllist, count);
				if (alllist.size() < 1) {
					ClassSeachActivity.serach_hestory_bt.setVisibility(View.GONE);
				} else {
					ClassSeachActivity.serach_hestory_bt.setVisibility(View.VISIBLE);
				}
				ClassSeachActivity.serach_hestory.setAdapter(serachHistoryAdapter);
				SerachHistorySqlliteOpenHelper helper = new SerachHistorySqlliteOpenHelper(
						context);
				SQLiteDatabase readableDatabase = helper.getReadableDatabase();
				Cursor querys = readableDatabase.query(false,
						SerachHistorySqlliteOpenHelper.SQLITE_TABLE, null,
						null, null, null, null, null, null, null);
				int haha = alllist.size() - position;
				if (querys.moveToPosition(haha)) {

					serach_name = querys.getString(querys
							.getColumnIndex("serach_str"));

				}
				SQLiteDatabase readableDatabase2 = helper.getReadableDatabase();
				readableDatabase2.delete(
						SerachHistorySqlliteOpenHelper.SQLITE_TABLE,
						"serach_str=?", new String[] { serach_name + "" });

			}
		});
		serach_history_item_tt.setText(alllist.get(position));
		return view;
	}

}
